package cn.stronglink.asset.module.assetmanage.scrap.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;

import cn.stronglink.asset.model.AssetRecord;
import cn.stronglink.asset.model.ItAsset;
import cn.stronglink.asset.model.Scrap;
import cn.stronglink.asset.model.ScrapDetail;
import cn.stronglink.asset.module.assetRecord.mapper.AssetRecordMapper;
import cn.stronglink.asset.module.assetmanage.itasset.mapper.ItassetMapper;
import cn.stronglink.asset.module.assetmanage.itasset.vo.ItAssetVo;
import cn.stronglink.asset.module.assetmanage.scrap.mapper.ScrapMapper;
import cn.stronglink.suite.core.util.WebUtil;

@Service
@Transactional(rollbackFor=Exception.class) 
public class ScrapService {

	@Autowired
	private ScrapMapper scrapMapper;
	@Autowired
	private ItassetMapper itassetMapper;
	@Autowired
	private AssetRecordMapper assetRecordMapper;

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<Scrap> queryScrapList(Page<Scrap> page, Map<String, Object> params) {
		
		// 首先查询出所有报废单
		List<Scrap> scrapList =scrapMapper.queryScrapList(page,params);
		
		//再根据每个报废单号查询出所有明细，并且把每个明细的设备型号，所在机柜拼接存放到报废单中。
		for(int i=0;i<scrapList.size();i++){
			Scrap scrap=scrapList.get(i);
			Long scrapId=scrap.getId();
			String modelNames="";
			List<ScrapDetail> ScrapDetailList =scrapMapper.queryScrapDetailByParentId(scrapId);
			if(ScrapDetailList!=null&&ScrapDetailList.size()>0){
				if(ScrapDetailList.size()>1){
					for(ScrapDetail ScrapDetail:ScrapDetailList){
						modelNames+=ScrapDetail.getModelName()+"/";
					}
				}else{
					for(ScrapDetail ScrapDetail:ScrapDetailList){
						modelNames+=ScrapDetail.getModelName();
					}
				}
			}
			scrap.setModelNames(modelNames);
		}
		page.setRecords(scrapList);
		return page;
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Scrap queryScrapById(Long id) {
		// 首先查询出报废单 然后查询报废单下所有的报废单明细
		Scrap scrap =scrapMapper.queryScrapById(id);
//		List<ScrapDetail> list =scrapMapper.queryScrapDetailList(id);
//		scrap.setDetailList(list);
		return	scrap;
	}

	public void remove(Long id) {
		// 根据报废单id删除报废单以及报废单详情
		try {
			scrapMapper.remove(id);
			scrapMapper.removeDetail(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void edit(Scrap Scrap) {
		try {
			scrapMapper.updateScrap(Scrap);
			for(ScrapDetail detail :Scrap.getDetailList()){
				if (detail.getId() == null) {
					detail.setId(IdWorker.getId());
					detail.setStatus(2);
					detail.setAssetId(Long.parseLong(detail.getAssetId_()));
					detail.setRemark(detail.getDetailRemark());
					detail.setCreateBy(WebUtil.getCurrentUser());
					detail.setCreateTime(new Date());
					scrapMapper.insertScrapDetail(detail);		
				}else{
					detail.setUpdateTime(new Date());
					detail.setUpdateBy(WebUtil.getCurrentUser());
					detail.setRemark(detail.getDetailRemark());
					scrapMapper.updateScrapDetail(detail);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	public int create(Scrap scrap) {
		scrap.setStatus(1);
		scrap.setCreateBy(WebUtil.getCurrentUser());
		scrap.setCreateTime(new Date());
		int count =scrapMapper.insertScrap(scrap);	
		if(scrap.getDetailList()!=null&&scrap.getDetailList().size()>0){
			for(ScrapDetail detail:scrap.getDetailList()){
				detail.setId(IdWorker.getId());
				detail.setScrapId(scrap.getId());
				detail.setStatus(2);
				detail.setAssetId(Long.parseLong(detail.getAssetId_()));
				detail.setRemark(detail.getDetailRemark());
				detail.setCreateBy(WebUtil.getCurrentUser());
				detail.setCreateTime(new Date());
				scrapMapper.insertScrapDetail(detail);		
			}
		}
		return count;
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public int qryScrapCount() {
		return scrapMapper.qryScrapCount();	
	}

	public void doScrap(Scrap scrap) {
		scrap.setStatus(3);
		scrap.setUpdateBy(WebUtil.getCurrentUser());
		scrap.setUpdateTime(new Date());
		scrapMapper.updateScrap(scrap);	
		if(scrap.getDetailList()!=null&&scrap.getDetailList().size()>0){
			for(ScrapDetail detail:scrap.getDetailList()){
				ScrapDetail d= new ScrapDetail();
				d.setId(detail.getDetailId());
				d.setScrapId(scrap.getId());
				d.setStatus(1);
				d.setRemark(detail.getDetailRemark());
				d.setUpdateBy(WebUtil.getCurrentUser());
				d.setUpdateTime(new Date());
				scrapMapper.updateScrapDetail(d);		
				
				// 更新资产表中状态为已报废
				ItAsset asset = new ItAsset();
				asset.setId(Long.parseLong(detail.getAssetId_()));
				asset.setStatus(3);
				itassetMapper.updateItAsset(asset);
				
				//插入资产生命周期表记录
				AssetRecord re = new AssetRecord();
				re.setId(IdWorker.getId());
				re.setAssetId(Long.parseLong(detail.getAssetId_()));
				re.setType(8);
				re.setCreateBy(WebUtil.getCurrentUser());
				re.setCreateTime(new Date());
				assetRecordMapper.create(re);
				
			}
		}
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<ItAssetVo> querAssetListByParam(Page<ItAssetVo> page,Map<String, Object> params) {
		page.setRecords(scrapMapper.querAssetListByParam(page,params));
		return page;
	}

}
